<?php
//   +----------------------------------------------------------------------
//   | Copyright (c) 2015-2025 http://www.hdphp.cn All rights reserved.
//   | Licensed ( http://www.hdphp.cn/licenses/ )
//   | Author: Jack <sophia2152@qq.com>
//   | 官方网址: http://www.hdphp.cn
//   | 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用。
//   | 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
//   +----------------------------------------------------------------------
namespace app\dao\work;

use app\dao\BaseDao;
use app\model\work\WorkGroupChat;
use hdphp\traits\SearchDaoTrait;

/**
 * 企业微信群
 * Class WorkGroupChatDao
 * @package app\dao\work
 */
class WorkGroupChatDao extends BaseDao
{

    use SearchDaoTrait;

    /**
     * @return string
     */
    protected function setModel(): string
    {
        return WorkGroupChat::class;
    }

    /**
     * @param array $where
     * @return \crmeb\basic\BaseModel
     */
    public function groupChat(array $where)
    {
        return $this->getModel()->when(!empty($where['chat_id']), function ($query) use ($where) {
            $query->whereIn('owner', function ($query) use ($where) {
                $query->name('work_group_msg_task')->when(!empty($where['status']), function ($query) use ($where) {
                    $query->where('status', $where['status']);
                })->whereIn('chat_id', $where['chat_id'])->field('userid');
            });
        })->when(!empty($where['owner']), function ($query) use ($where) {
            $query->whereIn('owner', $where['owner']);
        })->when(!empty($where['name']), function ($query) use ($where) {
            $query->whereLike('name', '%' . $where['name'] . '%');
        })->when(!empty($where['chat_id']), function ($query) use ($where) {
            if (is_array($where['chat_id'])) {
                $query->whereIn('chat_id', $where['chat_id']);
            } else {
                $query->where('chat_id', $where['chat_id']);
            }
        });
    }
}
